import React from 'react'
import {
    Form,
    Input,
    Button,
} from 'antd-mobile'
import  {useHistory}  from  "react-router-dom"
import { userLogin } from "../../service/userService";


export default function Login() {
    let history = useHistory();
    const onFinish = async (values: any) => {
        let res = await userLogin(values);
        console.log(res);
        if (res.data.code == 200) {
            window.localStorage.setItem("token",res.data.token);
            history.push("/");
        } else {
            alert(res.data.msg);
        }


    }
    return (
        <Form
            layout='horizontal'
            onFinish={onFinish}
            footer={
                <Button block type='submit' color='primary' size='large'>
                    提交
                </Button>
            }
        >
            <Form.Header>用户名和密码登录</Form.Header>
            <Form.Item
                name='username'
                label='姓名'
                rules={[{ required: true, message: '姓名不能为空' }]}
            >
                <Input onChange={console.log} placeholder='请输入姓名' />
            </Form.Item>
            <Form.Item name='password' label='密码' help='密码只能数字字母长度6到10' rules={[{ required: true, message: '密码不能为空' }, { pattern: /^[0-9a-z]{6,10}$/, message: '密码格式不合法' }]}>
                <Input type='password' placeholder='请输入密码' />
            </Form.Item>
        </Form>
    )
}
